<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单编号" prop="repairNo">
        <el-input
          v-model="queryParams.repairNo"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['repair:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['repair:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['repair:order:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['repair:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单id" align="center" prop="repairId" />
      <el-table-column label="工单编号" align="center" prop="repairNo" />
     <div class="information-title"> 
       <el-table-column label="工单问题内容" align="center" prop="repairDescription" />
       </div>
      <el-table-column label="工单发起人" align="center" prop="userId" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人id_用户" align="center" prop="modifierId" />
      <el-table-column label="工单状态" align="center" prop="repairStatus" >
        <template slot-scope="scope"> 
         <span> {{getLabel(getStatus,scope.row.repairStatus,'dictValue','dictLabel') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['repair:order:edit']"
          >分派</el-button> -->
          
          <router-link :to="{path:'/dtails/?repairId='+repairId}">
          <!-- <router-link to="path:'/dtails"> -->
          <!-- <router-link to="/dtails"> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
             @click="handleDelete(scope.row)" 
            v-hasPermi="['repair:order:remove']"
          >查看</el-button>
          </router-link>
         
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
           @click="onInsert"
            v-hasPermi="['repair:order:edit']"
            
          >分派</el-button> 
           <!-- @click="handleUpdate(scope.row)" -->
            <!-- v-model="show" -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['repair:order:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单分派改数据库标题版对话框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="受理部门" prop="deptReceive">
          <el-input v-model="form.deptReceive" placeholder="销售部" />
        </el-form-item>
        <el-form-item label="受理人" prop="deptReceiveP">
          <el-input v-model="form.deptReceiveP" placeholder="诸葛亮" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisibleConfirm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder,getOrder01 } from "@/api/repair/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单分派改数据库标题版表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否删除时间范围
      daterangeCreateTime: [],
      // 模拟数据字典数据，类型数据
       
        // 模拟数据字典数据，状态数据
        getStatus: [
          // {dictValue: 0,dictLabel:'分派中'},
          {dictValue: 1,dictLabel:'工单受理中'},
          {dictValue: 2,dictLabel:'工单已派单'},
          {dictValue: 3,dictLabel:'待您反馈'},
          {dictValue: 4,dictLabel:'正在为您处理'},
          {dictValue: 5,dictLabel:'工单已关闭'}
        ],
        // 模拟自定义接口数据，自定义
    
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairNo: null,
        createTime: null,
        repairStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //测试前端返回文字
    getLabel(list, id, value, label) {
      console.log("到达了getlabel");
      	if (id != '' && Array.isArray(list) && list.length != 0){
          return !list.find(item => item[value] == id) ? id : list.find(item => item[value] == id)[label]
        } else {
          return id
        }
      },
//自己写的弹窗
onInsert(){
this.dialogVisible =true;
},
    /** 查询工单分派改数据库标题版列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listOrder(this.queryParams).then(response => {
        console.log(response);
        console.log(response.rows);
         console.log(response.data);
        // console.log(response.rows.repairStatus);
        // if(response.rows[2].repairStatus==0){
        //   this.response.rows[2].repairStatus=="未分派"
        // }
        
      
       
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        repairId: null,
        repairNo: null,
        repairPriority: null,
        repairTypeV1: null,
        repairTypeV2: null,
        repairDescription: null,
        repairPicture: null,
        isAnonymous: null,
        userId: null,
        createTime: null,
        modifierId: null,
        updateTime: null,
        repairStatus: 0,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.repairId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单分派改数据库标题版";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const repairId = row.repairId || this.ids
      getOrder01(repairId).then(response => {
        // this.form = response.data;
      console.log(response.data);
        this.open = true;
        this.title = "工单分派";
      });
    },
    
    dialogVisibleConfirm() {
      // 新增接口
      if (!this.allid) {
        this.$api.jurisdiction.addrole(this.ruleForm)
          .then(res => {
            // console.log(res,"新增")
            this.$message.success("添加成功");     //新增成功消息提示
            this.$refs.refRuleForm.resetFields(); //清空表格数据
            this.dialogVisible = false;           //关闭弹框
            this.tabList();                       //刷新列表
          })
          .catch(res => {
            this.$message.error("添加失败");
          });
      } else {
        // 修改接口
        let id = this.allid
        let params = {
          roleName:this.ruleForm.roleName,
          roleDesc:this.ruleForm.roleDesc,
        }
        this.$api.jurisdiction.edtrole(id,params)
          .then(res => {
            console.log(res,"修改")
            this.$message.success("修改成功");
            this.$refs.refRuleForm.resetFields();
            this.dialogVisible = false;
            this.tabList();
          })
          .catch(res => {
            this.$message.error("修改失败");
          });
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.repairId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
       this.repairId = row.repairId
      // const repairIds = row.repairId || this.ids;
      // this.$modal.confirm('是否确认删除工单分派改数据库标题版编号为"' + repairIds + '"的数据项？').then(function() {
      //   return delOrder(repairIds);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('repair/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
